package com.lmy.springboot_rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitmq配置类
 * 用于配置交换机、队列及其绑定关系等
 */
@Configuration
public class DirectRabbitmqConfiguration {

    /**
     * ========================配置交换机========================
     */
    @Bean
    public DirectExchange directExchange() {
        //配置交换机 direct_order_exchange(分发模式为direct)，持久化且不自动删除
        return new DirectExchange("direct_order_exchange", true, false);
    }

    /**
     * ========================配置队列========================
     */
    @Bean
    public Queue sms_direct_queue() {
        //配置队列 sms_direct_queue，持久化
        return new Queue("sms_direct_queue", true);
    }

    @Bean
    public Queue email_direct_queue() {
        //配置队列 email_direct_queue，持久化
        return new Queue("email_direct_queue", true);
    }

    @Bean
    public Queue message_direct_queue() {
        //配置队列 message_direct_queue，持久化
        return new Queue("message_direct_queue", true);
    }

    /**
     * ========================配置交换机和队列的绑定关系========================
     */
    @Bean
    public Binding smsDirectBinding() {
        //将队列 sms_direct_queue 绑定到 交换机direct_order_exchange 指定routingKey
        return BindingBuilder.bind(sms_direct_queue()).to(directExchange()).with("sms");
    }

    @Bean
    public Binding emailDirectBinding() {
        //将队列 email_direct_queue 绑定到 交换机direct_order_exchange
        return BindingBuilder.bind(email_direct_queue()).to(directExchange()).with("email");
    }

    @Bean
    public Binding messageDirectBinding() {
        //将队列 message_direct_queue 绑定到 交换机direct_order_exchange
        return BindingBuilder.bind(message_direct_queue()).to(directExchange()).with("message");
    }
}
